package com.fstec.ks.master.impl;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;

import java.util.concurrent.TimeUnit;

public class ConnectListener implements ChannelFutureListener {
	private NettyClient client;

	public ConnectListener(NettyClient client){
		this.client=client;
	}

	@Override
	public void operationComplete(ChannelFuture future) throws Exception {
		if (future.isSuccess()) {
			client.onConnectSuccess();
		} else {
			client.onConnectFail();
			final EventLoop loop = future.channel().eventLoop();
			loop.schedule(new Runnable() {
				@Override
				public void run() {
					System.err.println("服务端连接不上，开始重连操作...");
					client.connect(false);
				}
			}, 10L, TimeUnit.SECONDS);
		}
	}
}
